#include "app_mqtt.h"

static MQTTClient mqttClient;
static MQTTClient_message msg=MQTTClient_message_initializer;
static  MQTTClient_connectOptions options = MQTTClient_connectOptions_initializer;

static  int (*app_mqtt_callback)(char *message)  ;

/**
 * @brief 失联回调
 */
void connectionLost_callback(void *context, char *cause)
{
    log_info("connectionLost_callback  cause %s", cause);

    //重新连接
    int ret = MQTTClient_connect(mqttClient, &options);
    if (ret != MQTTCLIENT_SUCCESS)
    {   
        perror("MQTTClient_connect failed ");
          MQTTClient_destroy(&mqttClient);
          return;
    }

    // 订阅主题
    ret = MQTTClient_subscribe(mqttClient, MQTT_RECV_TOPIC, 1);
    {
        perror("MQTTClient_subscribe failed ");
        MQTTClient_destroy(&mqttClient);
        return ;
    }

}

/**
 * @brief 消息达到回调
 */
int messageArrived_callback(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
     log_info("messageArrived_callback  payload %s", message->payload);

    int ret= app_mqtt_callback(message->payload);
    if(ret==0)
    {  //处理成功
        MQTTClient_freeMessage(&message);
        MQTTClient_free(topicName);
        return 1;
    }
    else{
        return 0; //处理失败
    }

}

/**
 * @brief 发送完成回调
 */
void deliveryComplete_callback(void *context, MQTTClient_deliveryToken dt)
{
    log_info("deliveryComplete_callback  dt %d", dt);
}

/**
 * @brief 初始化mqtt
 *          1创建客户端
            2设置回调 setCallback
            3连接broker
            4订阅主题

 */
int app_mqtt_init()
{
    // 1创建客户端
    int ret = MQTTClient_create(&mqttClient, MQTT_BROKE_URL, MQTT_CLIENT_ID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
    if (ret != MQTTCLIENT_SUCCESS)
    {
        perror("MQTTClient_create failed ");
        return -1;
    }

    //  2设置回调 setCallback

    ret = MQTTClient_setCallbacks(mqttClient, NULL, connectionLost_callback, messageArrived_callback, deliveryComplete_callback);
    if (ret != MQTTCLIENT_SUCCESS)
    {
        perror("MQTTClient_setCallbacks failed ");
        MQTTClient_destroy(&mqttClient);
        return -1;
    }
    //             3连接broker

    ret = MQTTClient_connect(mqttClient, &options);
    if (ret != MQTTCLIENT_SUCCESS)
    {   
        perror("MQTTClient_connect failed ");
          MQTTClient_destroy(&mqttClient);
        return -1;
    }

    //             4订阅主题
    ret = MQTTClient_subscribe(mqttClient, MQTT_RECV_TOPIC, 1);
    if(ret != MQTTCLIENT_SUCCESS){
        perror("MQTTClient_subscribe failed ");
        MQTTClient_destroy(&mqttClient);
        return -1;
    }

    return 0;
}

/**
 * @brief 发送mqtt消息
 */
int app_mqtt_send(char *message)
{   
    
    msg.payload = message;
    msg.payloadlen = strlen(message);
    msg.qos = MQTT_QOS;

    int ret= MQTTClient_publishMessage(  mqttClient, MQTT_SEND_TOPIC, &msg, NULL);
    if(ret != MQTTCLIENT_SUCCESS)
    {
        perror("MQTTClient_publishMessage failed ");
        return -1;
    }
    return 0;
}

/**
 * @brief 注册接收数据的回调函数
 */
int app_mqtt_register_callback(int (*callback)(char *message))
{
    app_mqtt_callback = callback;
    return 0;
}

/**
 * @brief 销毁mqtt
 */
void app_mqtt_destroy()
{
    MQTTClient_disconnect(mqttClient, 1000);
    MQTTClient_destroy(&mqttClient);
}